package com.dyh.quickdemo.demos.thre;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.util.Date;

/*
@Author=Joey Wheeler
@Date=2020/10/19 15:18
*/
@Setter@Getter
public class Suicide implements Runnable{

    private Date deadTime;
    private int exeCount = 0;
    private Runnable runnable;

    public Suicide(Runnable runnable,Date deadTime){
        this.runnable = runnable;
        this.deadTime = deadTime;
    }

    @Override
    public void run() {

        while (!Thread.interrupted()){
            if (System.currentTimeMillis()>deadTime.getTime())
            {
                if (Thread.currentThread().isAlive()) {
                    Thread.currentThread().interrupt();
                }
            }
            try {
                this.runnable.run();
            } catch (Exception e) {
               // Thread.currentThread().interrupt();//不加这个会鬼畜
                e.printStackTrace();
            }
        }
    }

    /*
    @Override
    public void run() {

        while (!Thread.interrupted()){
            if (System.currentTimeMillis()>deadTime.getTime())
            {
                if (Thread.currentThread().isAlive()) {
                    Thread.currentThread().interrupt();
                }
            }
            try {
                do something
            } catch (Exception e) {
                // Thread.currentThread().interrupt();//不加这个会鬼畜
                e.printStackTrace();
            }
        }
    }
    */
}
